/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.trick.admin.task;

import it.trick.admin.model.ChannelModel;
import it.trick.admin.model.ChapterModel;
import it.trick.admin.model.ContentsBaseModel;
import it.trick.admin.model.PageModel;
import it.trick.admin.model.PreModel;
import it.trick.admin.service.CategoryService;
import it.trick.admin.service.ChannelService;
import it.trick.admin.service.ChapterService;
import it.trick.admin.service.PageService;
import it.trick.admin.service.PreService;
import java.util.List;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

/**
 *
 * @author takahara
 */
public class ChangeContentsIndexTask extends Service {

    private List<ContentsBaseModel> contentList;

    public void setup(List<ContentsBaseModel> contentList) {
        this.contentList = contentList;
    }

    @Override
    protected Task createTask() {
        return new Task<Void>() {
            @Override
            protected Void call() throws Exception {

                int index = 1;
                for (ContentsBaseModel model : contentList) {

                    if (model instanceof ChannelModel) {
                        ChannelModel channelModel = (ChannelModel) model;
                        channelModel.setIndex(index);
                        ChannelService.updateChannel(channelModel);

                    } else if (model instanceof ChapterModel) {
                        ChapterModel chapterModel = (ChapterModel) model;
                        chapterModel.setIndex(index);
                        ChapterService.changeChapterIndex(chapterModel);

                    } else if (model instanceof PageModel) {
                        PageModel pageModel = (PageModel) model;
                        pageModel.setIndex(index);
                        PageService.changePageIndex(pageModel);

                    } else if (model instanceof PreModel) {
                        PreModel PreModel = (PreModel) model;
                        PreModel.setIndex(index);
                        PreService.updatePre(PreModel);

                    } else {
                        ContentsBaseModel baseModel = (ContentsBaseModel)model;
                        baseModel.setIndex(index);
                        CategoryService.updateCategory(baseModel);
                    }

                    index++;
                }

                return null;
            }
        };
    }
}
